﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace B.S.BasicData.Api.Read.Filters
{
    /// <summary>
    ///  1.写全局异常过滤器        
    ///  2.注册全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<GlobalExceptionFilter> logger;

        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            this.logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            //你的代码能运行到此处,说明你的代码有错了.
            //你应该在这里把你的错误日志用Nlog记录下来
            logger.LogError("发生异常:"+ context.Exception.Message); //记录异常完成
            context.ExceptionHandled = true;
        }
    }
}
